package www.wen.com.common.lib.util;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Build;
import android.text.SpannableStringBuilder;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

public class NotificationUtil extends ContextWrapper {
    private NotificationManager mManager;
    private NotificationCompat.Builder builder25;
    private Notification.Builder builder26;
    public static final String sID = "channel_1";
    public static final String sName = "channel_name_1";

    private Context mContext;

    public NotificationUtil(Context context) {
        super(context);

        this.mContext = context;

        //设置点击通知栏的动作为启动另外一个广播
       /* Intent intent = new Intent(context, SettingActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
        pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);*/
    }

    public void sendNotification(String title, String content,int Icon) {
        if (Build.VERSION.SDK_INT >= 26) {
            createNotificationChannel();
            Notification notification = getNotification_26(title, content, Icon).build();
            getmManager().notify(getInt(), notification);
        } else {
            Notification notification = getNotification_25(title, content, Icon).build();
            getmManager().notify(getInt(), notification);
        }
    }

    private NotificationManager getmManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    private NotificationCompat.Builder getBuilder25() {
        if (builder25 == null) {
            builder25 = new NotificationCompat.Builder(mContext);
        }
        return builder25;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private Notification.Builder getBuilder26() {
        if (builder26 == null) {
            builder26 = new Notification.Builder(mContext, sID);
        }
        return builder26;
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel() {
        NotificationChannel channel = new NotificationChannel(sID, sName, NotificationManager.IMPORTANCE_HIGH);

        //是否绕过勿打扰模式
        channel.setBypassDnd(true);
        //是否允许呼吸灯闪烁
        channel.enableLights(true);
        //闪关灯的灯光颜色
        channel.setLightColor(Color.RED);
        //桌面launcher的消息角标
        channel.canShowBadge();
        //设置是否应在锁定屏幕上显示此频道的通知
        //channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        //是否允许震动
        channel.enableVibration(true);
        //先震动1秒，然后停止0.5秒，再震动2秒则可设置数组为：new long[]{1000, 500, 2000}
//        channel.setVibrationPattern(new long[]{1000,500,2000});
        //设置震动方式，延迟零秒，震动一秒，延迟一秒、震动一秒
//        channel.setVibrationPattern(new long[]{0, 1000, 1000, 1000});

        getmManager().createNotificationChannel(channel);
    }

    public NotificationCompat.Builder getNotification_25(String title, String content, int Icon) {

        // 以下是展示大图的通知
        /*Notification.BigPictureStyle style = new Notification.BigPictureStyle();
        style.setBigContentTitle("BigContentTitle");
        style.setSummaryText("SummaryText");
        style.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.location));

        // 以下是展示多文本通知
        Notification.BigTextStyle style1 = new Notification.BigTextStyle();
        style1.setBigContentTitle(title);
        style1.bigText(content);*/

        return getBuilder25()
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(Icon)
//                .setLargeIcon(BitmapFactory.decodeResource(getResources(), Icon))
//                .setContentIntent(pendingIntent)
//                .setStyle(new NotificationCompat.BigTextStyle().setBigContentTitle(title).bigText(content))
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setWhen(System.currentTimeMillis())
//                .setVibrate(new long[]{0, 1000, 1000, 1000})
//                .setStyle(style)
                .setAutoCancel(true);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getNotification_26(String title, String content,int Icon) {
        return getBuilder26()
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(Icon)
//                .setLargeIcon(zoomImg(BitmapFactory.decodeResource(getResources(), Icon),160,160))
//                .setStyle(new Notification.BigTextStyle().setBigContentTitle(title).bigText(content))
                .setWhen(System.currentTimeMillis())
                .setShowWhen(true);
//                .setVibrate(new long[]{0, 1000, 1000, 1000});
//                .setStyle(new Notification.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.logo)))
//                .setContentIntent(pendingIntent);
    }

    public void setTitle(SpannableStringBuilder title){
        if (Build.VERSION.SDK_INT >= 26) {
            Notification notification = getBuilder26().setContentTitle(title).build();
            getmManager().notify(getInt(), notification);
        } else {
            Notification notification = getBuilder25().setContentTitle(title).build();
            getmManager().notify(getInt(), notification);
        }
    }

    public void setText(String title){
        if (Build.VERSION.SDK_INT >= 26) {
            Notification notification = getBuilder26().setContentText(title).build();
            getmManager().notify(getInt(), notification);
        } else {
            Notification notification = getBuilder25().setContentText(title).build();
            getmManager().notify(getInt(), notification);
        }
    }

    public void setText(int id,String title,String text){
        if (Build.VERSION.SDK_INT >= 26) {
            Notification notification = getBuilder26().setContentTitle(title).setContentText(text).build();
            getmManager().notify(id, notification);
        } else {
            Notification notification = getBuilder25().setContentTitle(title).setContentText(text).build();
            getmManager().notify(id, notification);
        }
    }

    public void cancel(){
//        getmManager().cancel(0);
        getmManager().cancelAll();
    }

    private int getInt(){
        //以产生6位数随机数为例
        return  (int) ((Math.random() * 9 + 1) * 100000);
    }

}
